package cn.amoqi.springboot.rabbit.rabbit.confirm;

import cn.amoqi.springboot.rabbit.rabbit.ConfirmRabbitConfig;
import com.rabbitmq.client.impl.AMQImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ConfirmReceiver {
    public static final Logger logger = LoggerFactory.getLogger(ConfirmReceiver.class);


    @RabbitListener(queues = ConfirmRabbitConfig.QUEUE_NAME)
    public void handler(Message message, Channel channel) throws IOException {
        Object payload = message.getPayload();
        MessageHeaders headers = message.getHeaders();
        Long tag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        String msgId = (String) headers.get("spring_returned_message_correlation");
        /*//判断是否被消费
        if (redisTemplate.opsForHash().entries("mail_log").containsKey(msgId)) {
            //redis 中包含该 key，说明该消息已经被消费过
            logger.info(msgId + ":消息已经被消费");
            channel.basicAck(tag, false);//确认消息已消费
            return;
        }*/
        //redisTemplate.opsForHash().put("mail_log", msgId, "javaCoder");
        channel.basicAck(tag, false);
        logger.info("消息接收成功："+payload);
    }

}
